﻿
Imports microsoft.Data.SQLite

Public Class 登录

    Private Sub 登录_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        CheckBox1.Checked = True
        Dim user As String = My.Settings.lastuser
        Dim pwd As String = My.Settings.pwd1
        Dim updatetime As DateTime = My.Settings.lasttime
        If pwd = "" Or DateDiff("d", updatetime, Now) > 3 Then   '超过3天则需要重新输入密码
            TextBox1.Text = user
            TextBox2.Text = ""
        Else
            TextBox1.Text = user
            TextBox2.Text = DecryptString(pwd）
        End If
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If TextBox1.Text = "" Or TextBox2.Text = "" Then MsgBox("内容不能为空！") : Exit Sub
        Using connection As New SqliteConnection(_connString)
            Try
                connection.Open()
                ' 检查账号是否已存在 
                Dim checkSql As String = "SELECT 密码 FROM userlist WHERE 账号 = @account and 是否注销 = @isfalse"
                Using checkCmd As New SqliteCommand(checkSql, connection)
                    checkCmd.Parameters.AddWithValue("@account", TextBox1.Text.Trim())
                    checkCmd.Parameters.AddWithValue("@isfalse", "false")

                    Using reader As SqliteDataReader = checkCmd.ExecuteReader()
                        If reader.HasRows Then
                            ' 检查是否和数据库密码一致
                            reader.Read()
                            Dim password As String = DecryptString(reader.GetString(reader.GetOrdinal("密码")))
                            If TextBox2.Text.Trim() = password Then
                                Me.Hide()
                                ' OpenChildForm(Of 待办清单)()

                                主窗体.TableLayoutPanel1.Visible = True

                                ' 创建并添加目录树子窗体 
                                Dim childForm As New 目录树()
                                AddChildFormToTableLayoutPanel(主窗体.TableLayoutPanel1, childForm, 0, 0)

                                reader.Close()

                                ' 创建并添加待办清单子窗体 
                                Dim childForm2 As New 待办清单()
                                AddChildFormToTableLayoutPanel(主窗体.TableLayoutPanel1, childForm2, 2, 0)

                                主窗体.TableLayoutPanel1.ColumnStyles(0) = New ColumnStyle(SizeType.Absolute, 200)
                                主窗体.TableLayoutPanel1.ColumnStyles(1) = New ColumnStyle(SizeType.Absolute, 主窗体.Width - 750)
                                主窗体.TableLayoutPanel1.ColumnStyles(2) = New ColumnStyle(SizeType.Absolute, 550)

                                username = TextBox1.Text
                            Else
                                MsgBox("密码或账号不正确，请检查", vbOKOnly, "提示") : Exit Sub
                            End If


                            '是否记住密码
                            If CheckBox1.Checked Then
                                My.Settings.lastuser = username  ' 修改 User 作用域的配置项
                                My.Settings.pwd1 = EncryptString(password)
                                My.Settings.lasttime = Now
                                My.Settings.Save()
                            Else
                                My.Settings.lastuser = ""
                                My.Settings.pwd1 = EncryptString("")
                                My.Settings.Save()
                            End If
                        Else
                            MsgBox("密码或账号不正确，请检查", vbOKOnly, "提示") ： Exit Sub
                        End If

                    End Using


                End Using
            Catch ex As Exception
                MessageBox.Show($"操作失败: {ex.Message}")
            End Try
        End Using
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        End
    End Sub



End Class